var utils = getApp().helper, app = getApp(), api = getApp().api;
var app = getApp(), api = getApp().api, is_loading = !1, is_no_more = !0;
function secondToTimeStr(t) {
  if (t < 60) return "00:00:" + ((i = t) < 10 ? "0" + i : i);
  if (t < 3600) return "00:" + ((a = parseInt(t / 60)) < 10 ? "0" + a : a) + ":" + ((i = t % 60) < 10 ? "0" + i : i);
  if (3600 <= t) {
      var a, i, e = parseInt(t / 3600);
      return (e < 10 ? "0" + e : e) + ":" + ((a = parseInt(t % 3600 / 60)) < 10 ? "0" + a : a) + ":" + ((i = t % 60) < 10 ? "0" + i : i);
  }
}



Page({
  data: {
    WindowWidth: getApp().core.getSystemInfoSync().windowWidth,
    WindowHeight: getApp().core.getSystemInfoSync().windowHeight,
    tab: 1,
    sort: 1,
    coupon_list: [],
    copy: !1,
    scrollTop: '',
    rects: '',
    template: null,
    time_all: [],
    show_notice: -1,
    play: -1,
    time: 0,
    form: {
      number: 1
    },
    
    is_waimai: 1,
    waimai_tab: 0,
    plus:false,
    plus1:false,
    comment_list: [],
    comment_type:0,
    waimai_page:0,
    waimai_shop_page: [],
    is_more_comment: 1,
    carGoods:[],
    checked_attr: {},
    temporaryGood: {},
    attr_group_list:{}, 
    checked_attr:{},
    currentGood:{},
    check_num :0,
    total: {
      total_num: 0,
      total_price: 0
     },
    showWaimaiModal: 0,
    waimai_cat_index: 0,
    showCoupon: 0,
    gtime_list: null,
    goods_list: null,
    page: 1,
    loading_more: !1,
    status: !0,
    sort:0,
    goods_list1:null,
    mch_id:"",
    goods_list2:null,
    naver: "list",
    lipin_list:"",
    chaozhi_list:"",
    remai_list:""
  },
    callPhone: function (t) {
      getApp().core.makePhoneCall({
          phoneNumber: t.target.dataset.info
      });
  },
  soushuo:function(){
    wx.navigateTo({
      url: '/pages/search/search',
    })
  }
  ,
  //定位
  map_power: function () {
    var o = this;
    console.log(o,'位位')
    getApp().getConfig(function (t) {
      console.log(t,'位置')

        // var e = o.data.shop;
        var e= o.__data__.shop;
        void 0 !== e ? o.map_goto(e) : getApp().core.getSetting({
            success: function (t) {
              console.log(t,'位置2')
                t.authSetting["scope.userLocation"] ? o.map_goto(e) : getApp().getauth({
                    content: "需要获取您的地理位置授权，请到小程序设置中打开授权！",
                    cancel: !1,
                    author: "scope.userLocation",
                    success: function (t) {
                        t.authSetting["scope.userLocation"] && o.map_goto(e);
                    }
                });
            }
        });
    });
},
map_goto: function (t) {
  getApp().core.openLocation({
      latitude: parseFloat(t.latitude),
      longitude: parseFloat(t.longitude),
      address: t.address
  });
},
  // 砍价
  loadkjData: function() {
    var a = this;
    getApp().request({
        url: getApp().api.bargain.index,
        data: {
            page: 1,
            sort:1
        },
        success: function(o) {
          console.log(o,'砍价')
            if (0 == o.code) {
                var t = o.data.goods_list;
                a.setData({
                  kjgoods_list: t
                });
            } else a.showToast({
                title: o.msg
            });
        }
    });
},

share_return() {
  return false;
},








  classfenlei:function(){
    var id=this.options.mch_id;
    console.log(id);
    wx.redirectTo({
      url: '/mch/shop-cat/shop-cat?mch_id=' + id,
    })
  },
  jieshao:function(){
    var id=this.options.mch_id;
    console.log(id);
    wx.redirectTo({
      url: '/mch/shop-summary/shop-summary?mch_id=' + id,
    })
  },
  js:function(){
    var id=this.options.mch_id;
    console.log(id);
    wx.redirectTo({
      url: '/mch/institutional-community/institutional-community?mch_id=' + id,
    })
  }
,  zhibo:function(){
  var id=this.options.mch_id;
    console.log(id);
    wx.redirectTo({
      url: '/lives/admin/goods-list/goods-list?mch_id=' + id,
    })
  },
lipin:function(){
  var that=this;
  console.log(this.options.mch_id,'pppppp')
  getApp().request({
    url:getApp().api.integral.goods_list,
    data:{
      mch_id:this.options.mch_id
    },
    success:function(e){
      console.log(e.data.list,'lipin')
      that.setData({
        lipin_list:e.data.list
      })
    }
  })

},
kanjia:function(){
  var a = this;

  // console.log(this.options.mch_id,'pppppp')
  getApp().request({
    url: getApp().api.bargain.index,
    data:{
      mch_id:this.options.mch_id
    },
    success:function(o){
      console.log(o,'kanjia')
      if (0 == o.code) {
        var t = o.data.goods_list;
        a.setData({
          kjgoods_list: t
        });
    } else a.showToast({
        title: o.msg
    });
    }
  })
}
,









  onSelectSort:function(e){
    console.log(e,'eeeee')
    this.setData({
        sort:e.currentTarget.dataset.sort,
        page: 1,
        goods_list: [],
      

    })
   
    
},
quickNavigation: function() {
    this.setData({
        quick_icon: !this.data.quick_icon
    });
    this.data.store;
    var t = getApp().core.createAnimation({
        duration: 300,
        timingFunction: "ease-out"
    });
    this.data.quick_icon ? t.opacity(0).step() : t.translateY(-55).opacity(1).step(), 
    this.setData({
        animationPlus: t.export()
    });
},
loadData: function(t) {
    var a = this;
    getApp().request({
        url: getApp().api.miaosha.list,
        success: function(t) {
            if (0 == t.code) if (0 == t.data.list.length) {
                if (0 == t.data.next_list.length) return void getApp().core.showModal({
                    content: "暂无秒杀活动",
                    showCancel: !1,
                    confirmText: "返回首页",
                    success: function(t) {
                        t.confirm && getApp().core.switchTab({
                            url: "/pages/index/index"
                        });
                    }
                });
                a.setData({
                    goods_list: t.data.next_list.list,
                    ms_active: !0,
                    time_list: t.data.list,
                    next_list: t.data.next_list.list,
                    next_time: t.data.next_list.time
                });
            } else a.setData({
                time_list: t.data.list,
                next_list: "" == t.data.next_list ? [] : t.data.next_list.list,
                next_time: "" == t.data.next_list ? [] : t.data.next_list.time,
                ms_active: !1
            }), a.topBarScrollCenter(), a.setTimeOver(), a.loadGoodsList(!1);
            1 == t.code && getApp().core.showModal({
                title: "提示",
                content: t.msg,
                success: function() {
                    getApp().core.switchTab({
                        url: "/pages/index/index"
                    });
                },
                showCancel: !1
            });
        }
    });
},
setTimeOver: function() {
    var e = this;
    function t() {
        for (var t in e.data.time_list) {
            e.data.time_list[t].now_time = Date.parse(new Date())/1000;
            var a = e.data.time_list[t].begin_time - e.data.time_list[t].now_time, i = e.data.time_list[t].end_time - e.data.time_list[t].now_time;
            a = 0 < a ? a : 0, i = 0 < i ? i : 0, e.data.time_list[t].begin_time_over = secondToTimeStr(a), 
            e.data.time_list[t].end_time_over = secondToTimeStr(i);
            // e.data.time_list[t].now_time = e.data.time_list[t].now_time + 1;
        }
        e.setData({
            time_list: e.data.time_list
        });
    }
    t(), setInterval(function() {
        t();
    }, 1e3);
},
miaosha_next: function() {
    var t = this, e = t.data.time_list;
    e.forEach(function(t, a, i) {
        e[a].active = !1;
    }), t.setData({
        goods_list: null,
        ms_active: !0,
        time_list: e
    }), setTimeout(function() {
        t.setData({
            goods_list: t.data.next_list
        });
    }, 500);
},
topBarScrollCenter: function() {
    var t = this, a = 0;
    for (var i in t.data.time_list) if (t.data.time_list[i].active) {
        a = i;
        break;
    }
    t.setData({
        top_bar_scroll: 2 <= a ? a - 2 : 0
    });
},
topBarItemClick: function(t) {
    var a = this, i = t.currentTarget.dataset.index;
    for (var e in a.data.time_list) a.data.time_list[e].active = i == e;
    a.setData({
        time_list: a.data.time_list,
        loading_more: !1,
        page: 1,
        ms_active: !1,
        sort:0
    }), a.topBarScrollCenter(), a.loadGoodsList(!1);
},
loadGoodsList: function(a) {
    var i = this, t = !1;
    for (var e in i.data.time_list) {
        if (i.data.time_list[e].active) {
            t = i.data.time_list[e].start_time;
            break;
        }
        i.data.time_list.length == parseInt(e) + 1 && 0 == t && (t = i.data.time_list[0].start_time, 
        i.data.time_list[0].active = !0);
    }
    a ? i.setData({
        loading_more: !0
    }) : i.setData({
        goods_list: null
    }), getApp().request({
        url: getApp().api.miaosha.goods_list,
        data: {
            time: t,
            page: i.data.page,
            sort:i.data.sort
        },
        success: function(t) {
          // console.log(t)
            0 == t.code && (i.data.goods_list = a ? i.data.goods_list.concat(t.data.list) : t.data.list, 
            i.setData({
                loading_more: !1,
                goods_list: i.data.goods_list,
                page: t.data.list && 0 != t.data.list.length ? i.data.page + 1 : -1
            }));
        }
    });
},
// remaiclass:function(){
//   var a =this
//   getApp().request({
//     url: getApp().api.mch.hot_sale,
//     data:{
//       mch_id: a.data.mch_id,
//     },
//     success:function(res){
//       console.log(res)
//     }
//   })
// },
remaiclass:function(){
  var that =this;
  getApp().request({
    url: getApp().api.mch.hot_sale,
    data:{
       mch_id:that.options.mch_id,
    },
    success:function(res){
      console.log(res,'热卖')
      that.setData({
        remai_list:res.data.list
      })
    }
  })
},

chaozhiclass:function(){
  var that= this;
console.log(this.options.mch_id)
console.log(getApp().api.mch.super_value,'ssssssssssssssssssvvvvvvvvvvvvvvvvv')
console.log(getApp().api.mch.hot_sale,'hhhhhhhhhhhhhhhhotttttttttttttt')


console.log(getApp().api.mch.shop,'shopshop') 
console.log(getApp().api.mch) 
  getApp().request({
    url: getApp().api.mch.super_value,
    data:{
       mch_id:that.options.mch_id,
    },
    success:function(res){
      console.log(res,'超值')
      that.setData({
        chaozhi_list:res.data.list
      })
    }
  })
},

  onLoad: function (t) {
    this.lipin();
    this.kanjia();
    this.chaozhiclass();
    this.remaiclass();
    console.log(t.mch_id)
    var id=t.mch_id
    // this.setData({
    //   mch_id:id
    // })
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
    var statusbarH = t.statusBarHeight
    this.setData({
      statusbar: statusbarH
    })
    getApp().page.onLoad(this, t),this.loadData(t);
    t.page_id,
     this.setData({
      options: t
    });
    var a = this;
    if ("undefined" == typeof my) {
      if (t.scene) {
        var o = decodeURIComponent(t.scene);
        o && (o = utils.scene_decode(o)).mch_id && (t.mch_id = o.mch_id);
      }
    } else if (null !== getApp().query) {
      var e = getApp().query;
      getApp().query = null, t.mch_id = e.mch_id;
    }
    a.setData({
      tab: t.tab || 1,
      sort: t.sort || 1,
      mch_id: t.mch_id || !1,
      cat_id: t.cat_id || ""
    }), a.data.mch_id || getApp().core.showModal({
      title: "提示",
      content: "店铺不存在！店铺id为空"
    }), this.getShopNavbar(), this.getShopData();
  },
  quickNavigation: function () {
    this.setData({
      quick_icon: !this.data.quick_icon
    });
    this.data.store;
    var t = getApp().core.createAnimation({
      duration: 300,
      timingFunction: "ease-out"
    });
    this.data.quick_icon ? t.opacity(0).step() : t.translateY(-55).opacity(1).step(),
      this.setData({
        animationPlus: t.export()
      });
  },
  onReady: function () {
    getApp().page.onReady(this);
  },
  onShow: function () {
    getApp().page.onShow(this);
    require("../../components/diy/diy.js").init(this)
  },
  onHide: function () {
    getApp().page.onHide(this);
  },
  onUnload: function () {
    getApp().page.onUnload(this);
  },
  onPullDownRefresh: function () { },
  onReachBottom: function () {
    this.getGoodsList();
    this.getCommentList();
  },
  onShareAppMessage: function () {
    getApp().page.onShareAppMessage(this);
    var t = this;
    return {
      path: "/mch/shop/shop?user_id=" + getApp().getUser().id + "&mch_id=" + t.data.mch_id + '&store_id=' + (wx.getStorageSync("store_id") ? wx.getStorageSync("store_id") : 0),
      title: t.data.shop ? t.data.shop.name : "商城首页"
    };
  },
  onShareTimeline: function () {
    var t = this;
    return {
      title: t.data.shop ? t.data.shop.name : "商城首页",
      query: {
        user_id: getApp().getUser().id,
        mch_id: t.data.mch_id,
        store_id: wx.getStorageSync("store_id") ? wx.getStorageSync("store_id") : 0
      },
    }
  },
  onWaiMaiLoad: function () {
    var self = this;
    var cat = this.data.template[0].param.list[this.data.waimai_cat_index];
    var page = this.data.waimai_shop_page[this.data.waimai_cat_index];
    if (page && page.is_end) {
      return;
    }
    if (cat.goods_style == 0) {
      wx.showLoading({
        title: '加载中...',
      });
      var current_page = page && page.page ? page.page + 1 : 2;
      getApp().request({
        url: getApp().api.mch.cat.get_goods_list,
        data: {
          mch_id: this.data.mch_id,
          cat_id: cat.cat_id,
          limit: 30,
          page: current_page
        },
        success: function (res) {
          // console.log(res)
          var list = res.data.list;
          if (list.length > 0) {
            cat.goods_list = cat.goods_list.concat(list);
            var template = self.data.template;
            template[0].param.list[self.data.waimai_cat_index] = cat;
            self.setData({
              template: template
            })
          }
          var waimai_page = self.data.waimai_shop_page;
          waimai_page[self.data.waimai_cat_index] = {
            page: current_page,
            is_end: list.length == 30 ? false : true
          }
          self.setData({
            waimai_shop_page: waimai_page
          })
        },
        complete: function () {
          wx.hideLoading();
        }
      });
    }
  },
  kfuStart: function () {
    this.setData({
      copy: !0
    });
  },
  kfuEnd: function () {
    this.setData({
      copy: !1
    });
  },
  copyinfo: function (t) {
    getApp().core.setClipboardData({
      data: t.target.dataset.info,
      success: function (t) {
        getApp().core.showToast({
          title: "复制成功！",
          icon: "success",
          duration: 2e3,
          mask: !0
        });
      }
    });
  },
  callPhone: function (t) {
    getApp().core.makePhoneCall({
      phoneNumber: t.target.dataset.info
    });
  },

  getShopData: function () {
    var a = this,
      o = (a.data.current_page || 0) + 1,
      e = "shop_data_mch_id_" + a.data.mch_id,
      t = getApp().core.getStorageSync(e);
    t && a.setData({
      shop: t.shop
    }), getApp().core.showNavigationBarLoading(), a.setData({
      loading: !0
    }), getApp().request({
      url: getApp().api.mch.shop,
      data: {
        mch_id: a.data.mch_id,
        tab: a.data.tab,
        sort: a.data.sort,
        page: o,
        cat_id: a.data.cat_id,
        page_id: a.data.options.page_id
      },
      success: function (t) {
        // console.log(t)
        if (t.code == 0) {
          if (t.data.status && t.data.status == 'diy') {
            getApp().core.setNavigationBarTitle({
              title: t.data.info
            });
            a.setData({
              shop: t.data.shop,
              template: t.data.template,
              time_all:t.data.time_all,
              _navbar: t.data.navbar,
              _navigation_bar_color: t.data.navigation_bar_color,
              is_waimai: t.data.is_waimai,
              coupon_list: t.data.coupon_list,
            }),a.setTime();
            getApp().core.setStorageSync(e, t.data);
          } else {
            a.setData({
              shop: t.data.shop,
              coupon_list: t.data.coupon_list,
              hot_list: t.data.goods_list,
              goods_list1: t.data.goods_list,
              new_list: t.data.new_list,
              current_page: o,
              cs_icon: t.data.shop.cs_icon,
              _navbar: t.data.navbar,
              _navigation_bar_color: t.data.navigation_bar_color,
              is_waimai: t.data.is_waimai,
            });
            getApp().core.setStorageSync(e, t.data);
          }
        } else {
          getApp().core.showModal({
            title: "提示",
            content: t.msg,
            showCancel: !1,
            success: function (t) {
              t.confirm && getApp().core.switchTab({
                url: "/pages/index/index"
              });
            }
          });
        }
      },
      complete: function () {
        getApp().core.hideNavigationBarLoading(), a.setData({
          loading: !1
        });
        /* 添加浏览记录 begin */
        getApp().request({
          url: getApp().api.default.browse,
          data: {
              id: a.data.mch_id,
              type: 1
          },
          success: function(t) {
          }
        });
        /* 添加浏览记录 end */
      }
    });
  },

  getShopNavbar: function () {
    var a = this;
    getApp().request({
      url: getApp().api.mch.shop_navbar,
      data: {
        mch_id: a.data.mch_id,
        tab: a.data.tab,
      },
      success: function (t) {
        // console.log(t)
        if (t.code == 0) {
          getApp().core.setNavigationBarColor(t.data.navigation_bar_color)

            var navbar = t.data.navbar
            for (var tt in navbar.navs) {
              if (navbar.navs[tt].url == '/mch/shop/shop') {
                var str = navbar.navs[tt].new_url
                var url_query = {}
                if (str.indexOf('?') != -1) {
                  var data = str.slice(str.indexOf('?'), str.length)
                  var aParams = data.substr(1).split('&')
                  for (var i = 0; i < aParams.length; i++) {
                    var aParam = aParams[i].split('=')
                    url_query[aParam[0]] = aParam[1]
                  }
                  if (url_query.mch_id == a.data.mch_id && url_query.tab == a.data.tab) {
                    navbar.navs[tt].active = 1;
                  }
                }else {
                  navbar.navs[tt].active = 1;
                }
              }
            }
            a.setData({
              _navbar: navbar,
              _navigation_bar_color: t.data.navigation_bar_color,
            }); 
        } else {
          getApp().core.showModal({
            title: "提示",
            content: t.msg,
            showCancel: !1,
            success: function (t) {
              t.confirm && getApp().core.switchTab({
                url: "/pages/index/index"
              });
            }
          });
        }
      },
      complete: function () {
      }
    });
  },
  onPageScroll: function (res) {
    if (this.data.tab == 2) {
      this.setData({
        scrollTop: res.scrollTop
      })
    }
  },
  goToShopDetail: function () {
    getApp().core.redirectTo({
      url: "/mch/shop-summary/shop-summary?mch_id=" + this.data.shop.id
    });
  },
  goToScanCodePay: function () {
    getApp().core.redirectTo({
      url: "/pages/scan-code-pay/scan-code-pay?mch_id=" + this.data.shop.id
    });
  },
  getGoodsList: function () {
    var a = this;
    if (3 != a.data.tab && !a.data.loading && !a.data.no_more) {
      a.setData({
        loading: !0
      });
      var o = (a.data.current_page || 0) + 1;
      getApp().request({
        url: getApp().api.mch.shop,
        data: {
          mch_id: a.data.mch_id,
          tab: a.data.tab,
          sort: a.data.sort,
          page: o,
          cat_id: a.data.cat_id,
          page_id: a.data.options.page_id
        },
        success: function (t) {
          // console.log(t+"a")
          0 == t.code && (1 == a.data.tab && (t.data.goods_list && t.data.goods_list.length ? (a.data.hot_list = a.data.hot_list.concat(t.data.goods_list),
            a.setData({
              hot_list: a.data.hot_list,
              current_page: o
            })) : a.setData({
              no_more: !0
            })), 2 == a.data.tab && (t.data.goods_list && t.data.goods_list.length ? (a.data.goods_list = a.data.goods_list.concat(t.data.goods_list),
              a.setData({
                goods_list: a.data.goods_list,
                current_page: o
              })) : a.setData({
                no_more: !0
              })));
        },
        complete: function () {
          a.setData({
            loading: !1
          });
        }
      });
    }
  },
  goToShopList: function () {
    wx.redirectTo({
      url: '/pages/shop-list/shop-list?shop=' + JSON.stringify(this.data.shop),
    });
  },
  showNotice: function (t) {
    this.setData({
      show_notice: t.currentTarget.dataset.index
    });
  },
  closeNotice: function () {
    this.setData({
      show_notice: -1
    });
  },
  waimaiCatBind: function(t) {
        var a = t.currentTarget.dataset.index
        // var o = this, e = t.currentTarget.dataset.template, a = t.currentTarget.dataset.index, i = o.data.template;
        // i[e].param.cat_index = a, o.setData({
        //     template: i
        // });
        this.setData({
          waimai_cat_index: a
        })
    },
  waimaiMenuClick: function (t) {
    var waimai_tab = t.currentTarget.dataset.current;
    
    if(waimai_tab==1){
     wx.navigateTo({
       url: '/pages/scan-code-pay/scan-code-pay?mch_id=' + this.data.mch_id
     })
     return ;
    }else if(waimai_tab==2){
      this.setData({
        comment_list: [],
        waimai_page:0,
        is_more_comment:1
      });
      
      this.getCommentList();
    }
    this.setData({
      waimai_tab: waimai_tab
    });
  },
  commentType: function (t) {
    var comment_type = t.currentTarget.dataset.comment_type;
    
    this.setData({
      comment_type: comment_type,
      comment_list: [],
      waimai_page:0,
      is_more_comment:1
    });
    this.getCommentList();
  },
  getCommentList: function() {
    var e = this;
   
    if(e.data.waimai_tab == 2 && e.data.is_more_comment==1){
      wx.showLoading({
        title: "加载中",
      }),
      getApp().request({
        url: getApp().api.mch.mch_comment_list,
        data: {
           mch_id: e.data.mch_id,
           comment_type: e.data.comment_type,
           page: e.data.waimai_page,
        },
        success: function(t) {
          // console.log(t+"b")
          if(0 == t.code){
            var waimai_page = e.data.waimai_page+1;
             e.setData({
              // comment_count: t.data.comment_count,
              comment_list:  e.data.comment_list.concat(t.data.list),
              waimai_page: waimai_page
            });
            if(0 == t.data.list.length){
              e.setData({
                is_more_comment: !1
              })
            }        
          }
           
        },
        complete: function () {
          wx.hideLoading()
        }
      });

    }
   
},
commentPicView: function(t) {
  var o = t.currentTarget.dataset.index, e = t.currentTarget.dataset.picIndex;
  getApp().core.previewImage({
      current: this.data.comment_list[o].pic_list[e],
      urls: this.data.comment_list[o].pic_list
  });
},
waimaijia: function(t){
  var o = this;
  var  a = t.currentTarget.dataset;
  var template = this.data.template;
  for (var r in template) {
    if (template[r].type == 'waimai') {
    
      for(var s in template[r].param.list){
         for (var n in template[r].param.list[s].goods_list){
          var goods_list = template[r].param.list[s].goods_list[n];
          if(parseInt(goods_list.id) === parseInt(a.id)){
       
            var e = goods_list.num ? goods_list.num + 1 : 1;
            if (e > goods_list.attr[0].num) return void wx.showToast({
                title: "商品库存不足",
                image: "/images/icon-warning.png"
            });
            goods_list.num = e;

            var d = this.data.carGoods, c = 1, u = a.price ? a.price : goods_list.price;
            for (var g in d) {
                if (parseInt(d[g].goods_id) === parseInt(goods_list.id) && 1 === goods_list.attr.length) {
                    c = 0, d[g].num = e, d[g].goods_price = (d[g].num * d[g].price).toFixed(2);
                    break;
                }
                var p = a.index;
                if (d[p]) {
                    c = 0, d[p].num = d[p].num + 1, d[p].goods_price = (d[p].num * d[p].price).toFixed(2);
                    break;
                }
            }
            if (1 === c || 0 === d.length) {
                var h = goods_list.attr;
                d.push({
                    goods_id: parseInt(goods_list.id),
                    attr: h[0].attr_list,
                    goods_name: goods_list.name,
                    goods_price: u,
                    num: 1,
                    price: u
                });
            }
          }
         }
      }
    }
  }

  o.setData({
    carGoods: d,
    template: template
  }), this.carStatistics(o),this.catGoodsNum();
},
waimaijian: function(t){
  var o = this;
  var  a = t.currentTarget.dataset;
  var template = this.data.template;
  for (var r in template) {
    if (template[r].type == 'waimai') {
    
      for(var s in template[r].param.list){
         for (var n in template[r].param.list[s].goods_list){
          var goods_list = template[r].param.list[s].goods_list[n];
          if(parseInt(goods_list.id) === parseInt(a.id)){
           
            var e = goods_list.num ? goods_list.num - 1 : 1;
            goods_list.num = e;

            var d = this.data.carGoods, c = 1, u = a.price ? a.price : goods_list.price;
            for (var g in d) {
                if (parseInt(d[g].goods_id) === parseInt(goods_list.id) && 1 === goods_list.attr.length) {
                     d[g].num = e, d[g].goods_price = (d[g].num * d[g].price).toFixed(2);
                    break;
                }
                var p = a.index;
                if (d[p]) {
                    d[p].num = d[p].num + 1, d[p].goods_price = (d[p].num * d[p].price).toFixed(2);
                    break;
                }
            }
          }
         }
      }
    }
  }

  o.setData({
    carGoods: d,
    template: template
  }), this.carStatistics(o),this.catGoodsNum();
},
waimaiGoodNumChange: function(t) {
  console.log(t);
  var o = this,
  a = parseInt(t.detail.value) ? parseInt(t.detail.value) : 0,
  i = t.target.dataset.id ? parseInt(t.target.dataset.id) : o.data.currentGood.id,
  r = o.data.carGoods,
  e = a, d = 0, c = "";;
  if(a<=0){
    return;
  }
  var template = this.data.template;
  for (var g in template) {
    if (template[g].type == 'waimai') {
    
      for(var s in template[g].param.list){
         for (var n in template[g].param.list[s].goods_list){
          var goods_list = template[g].param.list[s].goods_list[n];
         
          var p = parseInt(goods_list.use_attr);
          var C = parseInt(goods_list.id);
           
            if ( C === i && 0 === p) {
              var h = parseInt(goods_list.attr[0].num);
          
              h < a && (wx.showToast({
                  title: "商品库存不足",
                  image: "/images/icon-warning.png"
              }), e = h), goods_list.num = e, d = p;
          }
          if (C === i && 1 === p) {
              var _ = this.data.temporaryGood;
              _.num < a && (wx.showToast({
                  title: "商品库存不足",
                  image: "/images/icon-warning.png"
              }), e = _.num),  goods_list.num = e, d = p, this.setData({
                  check_goods_price: (e * _.price).toFixed(2)
              });
          }
           
          
         }
      }
    }
  }


  var m = 0;
  for (var l in r) {
      if ((C = parseInt(r[l].goods_id)) === i && 0 === d && (r[l].num = e, r[l].goods_price = (e * r[l].price).toFixed(2)), 
      C === i && 1 === d) {
          var v = o.data.checked_attr, f = r[l].attr, k = [];
          for (var u in f) k.push([ f[u].attr_id, i ]);
          k.sort().join() === v.sort().join() && (r[l].num = e, r[l].goods_price = (e * r[l].price).toFixed(2));
      }
      C === i && (m += r[l].num);
  }

  this.setData({
    carGoods: r,
    template: template
  }), this.carStatistics(o),this.catGoodsNum();;
},
showWaimaiDialogBtn: function(t) {
  var a = this, i = t.currentTarget.dataset;
  wx.showLoading({
    title: '加载中...',
  })
  getApp().request({
      url: getApp().api.default.goods,
      data: {
          id: i.id
      },
      success: function(t) {
        // console.log(t)
          0 == t.code && (a.setData({
              currentGood: t.data,
              goods_name: t.data.name,
              attr_group_list: t.data.attr_group_list,
              showWaimaiModal: !0
          }), a.resetData(), a.updateData(), a.checkAttrNum());
      },
      complete: function () {
        wx.hideLoading()
      }
  });
},
showMaimaicloseBtn:function (t){
      this.setData({
        showWaimaiModal: !1
      })
},
resetData: function() {
  this.setData({
      checked_attr: [],
      check_num: 0,
      check_goods_price: 0,
      temporaryGood: {
          price: "0.00",
          num: 0
      }
  });
},
updateData: function() {
  var t = this, r = t.data.currentGood, a = t.data.carGoods, i = JSON.parse(r.attr), o = r.attr_group_list;
  for (var e in i) {
      var n = [];
      for (var s in i[e].attr_list) n.push([ i[e].attr_list[s].attr_id, r.id ]);
      for (var d in a) {
          var c = [];
          for (var u in a[d].attr) c.push([ a[d].attr[u].attr_id, a[d].goods_id ]);
          if (n.sort().join() === c.sort().join()) {
              for (var _ in o) for (var p in o[_].attr_list) for (var h in n) {
                  if (parseInt(o[_].attr_list[p].attr_id) === parseInt(n[h])) {
                      o[_].attr_list[p].checked = !0;
                      break;
                  }
                  o[_].attr_list[p].checked = !1;
              }
              var g = {
                  price: a[d].price
              };
              return void t.setData({
                  attr_group_list: o,
                  check_num: a[d].num,
                  check_goods_price: a[d].goods_price,
                  checked_attr: n,
                  temporaryGood: g
              });
          }
      }
  }
},
checkUpdateData: function(t) {
  var r = this, a = r.data.carGoods;
  for (var i in a) {
      var o = [];
      for (var e in a[i].attr) o.push([ a[i].attr[e].attr_id, a[i].goods_id ]);
      o.sort().join() === t.sort().join() && r.setData({
          check_num: a[i].num,
          check_goods_price: a[i].goods_price
      });
  }
},
attrClick: function(t) {
  var r = this, a = parseInt(t.target.dataset.groupId), i = parseInt(t.target.dataset.id), o = r.data.attr_group_list, e = r.data.currentGood, n = JSON.parse(e.attr), s = [];
  for (var d in o) if (o[d].attr_group_id == a) for (var c in o[d].attr_list) {
      (G = o[d].attr_list[c]).attr_id == i && !0 !== G.checked ? G.checked = !0 : G.checked = !1;
  }
  var u = [];
  for (var d in o) for (var c in o[d].attr_list) {
      !0 === (G = o[d].attr_list[c]).checked && (u.push([ G.attr_id, e.id ]), s.push(G.attr_id));
  }
  var _ = JSON.parse(e.attr), p = r.data.temporaryGood;
  for (var h in _) {
      var g = [];
      for (var l in _[h].attr_list) g.push([ _[h].attr_list[l].attr_id, e.id ]);
      if (g.sort().join() === u.sort().join()) {
          if (0 === parseInt(_[h].num)) return;
          p = parseFloat(_[h].price) ? {
              price: parseFloat(_[h].price).toFixed(2),
              num: _[h].num
          } : {
              price: parseFloat(e.price).toFixed(2),
              num: _[h].num
          };
      }
  }
  var v = [];
  for (var d in console.log(s), n) {
      g = [];
      var f = 0;
      for (var c in n[d].attr_list) getApp().helper.inArray(n[d].attr_list[c].attr_id, s) || (f += 1), 
      g.push(n[d].attr_list[c].attr_id);
      0 === n[d].num && f <= 1 && v.push(g);
  }
  var m = s.length, k = [];
  if (o.length - m <= 1) for (var d in s) for (var c in v) if (getApp().helper.inArray(s[d], v[c])) for (var h in v[c]) v[c][h] !== s[d] && k.push(v[c][h]);
  for (var d in console.log(k), console.log(s), o) for (var c in o[d].attr_list) {
      var G = o[d].attr_list[c];
      getApp().helper.inArray(G.attr_id, k) && !getApp().helper.inArray(G.attr_id, s) ? G.is_attr_num = !0 : G.is_attr_num = !1;
  }
  this.resetData(), this.checkUpdateData(u), r.setData({
      attr_group_list: o,
      temporaryGood: p,
      checked_attr: u
  });
},
checkAttrNum: function() {
  var t = this, r = t.data.attr_group_list, a = JSON.parse(t.data.currentGood.attr), i = t.data.checked_attr, o = [];
  for (var e in i) o.push(i[e][0]);
  for (var e in a) {
      var n = [], s = 0;
      for (var d in a[e].attr_list) {
          var c = a[e].attr_list[d].attr_id;
          getApp().helper.inArray(c, o) ? s += 1 : n.push(c);
      }
      if (r.length - s == 1 && 0 == a[e].num) for (var u in r) for (var _ in r[u].attr_list) {
          var p = r[u].attr_list[_];
          getApp().helper.inArray(p.attr_id, n) && (p.is_attr_num = !0);
      }
  }
  t.setData({
      attr_group_list: r
  });
},
waimaiguigejia: function(t) {
  var r = this, a = r.data.attr_group_list, i = r.data.checked_attr, o = r.data.currentGood;
 
        if (i.length === a.length) {
            var e = r.data.check_num ? r.data.check_num + 1 : 1, n = JSON.parse(o.attr);
            for (var s in n) {
                var d = [];
                for (var c in n[s].attr_list) if (d.push([ n[s].attr_list[c].attr_id, o.id ]), d.sort().join() === i.sort().join()) {
                    var u = n[s].price ? n[s].price : o.price, _ = n[s].attr_list;
                    if (e > n[s].num) return void wx.showToast({
                        title: "商品库存不足",
                        image: "/images/icon-warning.png"
                    });
                }
            }
            var p = r.data.carGoods, h = 1, g = parseFloat(u * e).toFixed(2);
            for (var l in p) {
                var v = [];
                for (var f in p[l].attr) v.push([ p[l].attr[f].attr_id, p[l].goods_id ]);
                if (v.sort().join() === i.sort().join()) {
                    h = 0, p[l].num = p[l].num + 1, p[l].goods_price = parseFloat(u * p[l].num).toFixed(2);
                    break;
                }
            }    
          
            1 !== h && 0 !== p.length || p.push({
                goods_id: o.id,
                attr: _,
                goods_name: o.name,
                goods_price: u,
                num: 1,
                price: u
            }), r.setData({
                carGoods: p,
                check_goods_price: g,
                check_num: e
            }), this.carStatistics(r), this.attrGoodStatistics();
        } else wx.showToast({
            title: "请选择规格",
            image: "/images/icon-warning.png"
        });
},
waimaiguigejian: function(t) {
 
  var r = this, a = r.data.checked_attr, i = r.data.carGoods, o = r.data.check_num ? --r.data.check_num : 1;
        r.data.currentGood;

        for (var e in i) {

            var n = [];
            for (var s in i[e].attr) n.push([ i[e].attr[s].attr_id, i[e].goods_id ]);

            if (n.sort().join() === a.sort().join()) return 0 < i[e].num && (i[e].num -= 1, 
            i[e].goods_price = parseFloat(i[e].num * i[e].price).toFixed(2)), 

            r.setData({
                carGoods: i,
                check_goods_price: i[e].goods_price,
                check_num: o
            }), this.carStatistics(r), this.attrGoodStatistics();
        }
},

carStatistics: function(t) {
  var o = t.data.carGoods, a = 0, i = 0;
 
  for (var r in o) a += o[r].num, i = parseFloat(i) + parseFloat(o[r].goods_price);
  var s = {
      total_num: a,
      total_price: i.toFixed(2)
  };
  0 === a && this.hideShoppingCart(t), t.setData({
      total: s
  });


},
catGoodsNum: function () {
 
  var template = this.data.template;
   for (var r in template) {
     if (template[r].type == 'waimai') {
     
       for(var s in template[r].param.list){
         template[r].param.list[s].cat_goods_num = 0;
         var cat_goods_num = 0;
          for (var n in template[r].param.list[s].goods_list){
             
           var goods_list = template[r].param.list[s].goods_list[n];
            var cat_goods_num = cat_goods_num + goods_list.num;

          }
          template[r].param.list[s].cat_goods_num = cat_goods_num;
       }
     }
   }

   this.setData({
     template: template
   });
},
hideShoppingCart: function() {
  this.setData({
      shoppingCartModel: !1
  });
},
shoppingCartListModel: function() {
  var t = this, o = (t.data.carGoods, t.data.shoppingCartModel);
  console.log(o), o ? t.setData({
      shoppingCartModel: !1
  }) : t.setData({
      shoppingCartModel: !0
  });
},
updateGoodNum: function() {
  var t = this.currentPage, o = t.data.quick_list, a = t.data.goods;
  if (o && a) for (var i in o) for (var r in o[i].goods) if (parseInt(o[i].goods[r].id) === parseInt(a.id)) {
      var s = o[i].goods[r].num, n = o[i].goods[r].num;
      t.setData({
          goods_num: n,
          goodNumCount: s
      });
      break;
  }
},
attrGoodStatistics: function() {
  var t = this, c = t.data.currentGood, a = t.data.carGoods, template = t.data.template, e = 0;
  for (var n in a) a[n].goods_id === c.id && (e += a[n].num);
  
  for (var r in template) {
    if (template[r].type == 'waimai') {
    
      for(var s in template[r].param.list){
        template[r].param.list[s].cat_goods_num = 0;
        var cat_goods_num = 0;
         for (var n in template[r].param.list[s].goods_list){
            
          var goods_list = template[r].param.list[s].goods_list[n];
          if(parseInt(goods_list.id) === parseInt(c.id)){
            goods_list.num =  e;
          }
          var cat_goods_num = cat_goods_num + goods_list.num;
         
         }
        
        template[r].param.list[s].cat_goods_num = cat_goods_num;

      }
    }
  }

  t.setData({
    template: template,
  });
},
buywaimainow: function(t) {
  var o = this, a = o.data.carGoods;
 
  for (var i = a.length, r = [], s = [], n = 0; n < i; n++) 0 != a[n].num && (s = {
      goods_id: a[n].goods_id,
      num: a[n].num,
      attr: a[n].attr
  }, r.push(s));
  var e = [];
  e.push({
      mch_id: o.data.mch_id,
      goods_list: r
  }), getApp().core.navigateTo({
      url: "/pages/new-order-submit/new-order-submit?mch_list=" + JSON.stringify(e)
  }), this.clearShoppingCart();
},
clearShoppingCart: function(t) {
  var t = this, template = t.data.template;
  
  for (var r in template) {
    if (template[r].type == 'waimai') {
    
      for(var s in template[r].param.list){
        template[r].param.list[s].cat_goods_num = 0;
         for (var n in template[r].param.list[s].goods_list){
          var goods_list = template[r].param.list[s].goods_list[n];   
            goods_list.num = 0;
         }
      }
    }
  }

  t.setData({
      goodsModel: !1,
      carGoods: [],
      total: {
          total_num: 0,
          total_price: 0
      },
      check_num: 0,
      template: template,
      currentGood: [],
      checked_attr: [],
      check_goods_price: 0,
      temporaryGood: {},
      goodNumCount: 0,
      goods_num: 0
  });
},
receive: function(t) {
  var o = this, e = t.currentTarget.dataset.index;
  var id = o.data.coupon_list[e].id
  getApp().core.showLoading({
      mask: !0
  }), getApp().request({
      url: getApp().api.coupon.receive,
      data: {
          id: id
      },
      success: function(t) {
        // console.log(t)
        if (0 == t.code) {
          var coupon_list = o.data.coupon_list;
          coupon_list[e].is_receive = 1;
          o.setData({
            coupon_list: coupon_list
          });
        }else {
          wx.showToast({
            title: t.msg,
            image: "/images/icon-warning.png"
          });
        }
      },
      complete: function() {
          getApp().core.hideLoading();
      }
  });
},
updateCoupon: function () {
  var that = this;
  that.setData({
    showCoupon: !that.data.showCoupon
  })
}

});
